/*==============================================================================
FILE NAME: Figure_A8.do
CREATED: 12 June 2025
==============================================================================*/

**Figure A8

/* Set directory if working independently through code
if c(username)=="" { //insert username
	global rootdir "" // insert root path
	global processed_data "$rootdir/processed_data" 
	global figures "$rootdir/output/figures"  // Define global paths for replication package
} 
*/

//create indicator for RNs that were ever the subject of a complaint during sample period and re-run complaint air inv as treatment models
use "$processed_data/Air_Panel.dta", clear

drop if never_air_inv==1

egen t = group(year month)

xtset RN_id t

// Create binary indicators for types of investigation
gen p_nocomplaint_only = 0
replace p_nocomplaint_only = 1 if p_air_nocomplaint_inv == 1 & p_air_complaint_inv == 0
replace p_nocomplaint_only = . if p_air_nocomplaint_inv == . | p_air_complaint_inv == .

gen p_only_complaint_inv = 0
replace p_only_complaint_inv = 1 if p_air_complaint_inv == 1 & p_air_nocomplaint_inv == 0
replace p_only_complaint_inv = . if p_air_nocomplaint_inv == . | p_air_complaint_inv == .

// Generate event-study leads/lags for several outcomes
foreach y in p_air_complaint_inv p_air_nocomplaint_inv p_nocomplaint_only p_air_inv p_air_nov p_air_noe p_only_complaint_inv p_air_incident p_ee_incident {
forv h = 0/12 {
gen `y'_`h' = f`h'.`y' - l1.`y'
}

forv h = 2/12 {
gen `y'_neg`h' = l`h'.`y' - l1.`y'
}
}

// STEP 2: Identify "ever incident" RNs (treatment exposure) for filtering

egen RN_year = group(RN year)

xtset RN_id t
keep RN_id p_air_incident
sort RN_id
unique RN_id if p_air_incident==1
collapse (max) p_air_incident, by(RN_id)
rename p_air_incident ever_air_incident
label var ever_air_incident "=1 if RN_id was ever the subject of a complaint during sample period"
sort RN_id
unique RN_id if ever_air_incident==1
save "$processed_data/temp.dta", replace

// STEP 3: Reload panel, merge in "ever_air_incident", re-generate event-study variables

use "$processed_data/Air_Panel.dta", clear
drop if never_air_inv==1

egen t = group(year month)

xtset RN_id t

// Recreate indicators
gen p_nocomplaint_only = 0
replace p_nocomplaint_only = 1 if p_air_nocomplaint_inv == 1 & p_air_complaint_inv == 0
replace p_nocomplaint_only = . if p_air_nocomplaint_inv == . | p_air_complaint_inv == .

gen p_only_complaint_inv = 0
replace p_only_complaint_inv = 1 if p_air_complaint_inv == 1 & p_air_nocomplaint_inv == 0
replace p_only_complaint_inv = . if p_air_nocomplaint_inv == . | p_air_complaint_inv == .

// Recreate leads and lags
foreach y in p_air_complaint_inv p_air_nocomplaint_inv p_nocomplaint_only p_air_inv p_air_nov p_air_noe p_only_complaint_inv p_air_incident p_ee_incident {
forv h = 0/12 {
gen `y'_`h' = f`h'.`y' - l1.`y'
}

forv h = 2/12 {
gen `y'_neg`h' = l`h'.`y' - l1.`y'
}
}

egen RN_year = group(RN year)

sort RN_id
merge m:1 RN_id using "$processed_data/temp.dta", nogenerate
save "$processed_data/Air_Panel_Investigated_Clean_ever_air_incident.dta", replace

//NOV as outcome, complaint air inv as treatment
use "$processed_data/Air_Panel_Investigated_Clean_ever_air_incident.dta", clear
keep if ever_air_incident==1

// Initialize storage
cap drop b u d se Years Zero
gen Years = _n-13 if _n<=25
gen Zero = 0 if _n <=25
gen b = 0
gen se = 0
gen u = 0 
gen d = 0

//Regressions for post-complaint outcomes 
foreach h in 0 1 2 3 4 5 6 7 8 9 10 11 12 {
reghdfe p_air_nov_`h' p_air_complaint_inv, absorb(RN_year t) cluster(RN_id)
replace b = _b[p_air_complaint_inv] if Years == `h'
replace se = _se[p_air_complaint_inv] if Years == `h'
replace u = (_b[p_air_complaint_inv] + 1.96*_se[p_air_complaint_inv]) if Years == `h'
replace d = (_b[p_air_complaint_inv] - 1.96*_se[p_air_complaint_inv]) if Years == `h'
}

//Regressions for pre-complaint
foreach h in 2 3 4 5 6 7 8 9 10 11 12 {
reghdfe p_air_nov_neg`h'  p_air_complaint_inv, absorb(RN_year t) cluster(RN_id)
replace b = _b[p_air_complaint_inv] if Years == -`h'
replace se = _se[p_air_complaint_inv] if Years == -`h'
replace u = (_b[p_air_complaint_inv] + 1.96*_se[p_air_complaint_inv]) if Years == -`h'
replace d = (_b[p_air_complaint_inv] - 1.96*_se[p_air_complaint_inv]) if Years == -`h'
}
preserve
keep if Years != .
keep b u d se Years Zero
export delimited "$point_estimates/Point_Estimates_Figure_A8_Panel_A.csv", replace
graph set window fontface "Times New Roman"
twoway(rarea u d Years, col(navy) fint(inten20) lwidth(0) lpattern(solid))(line b Years, lcolor(navy) lpattern(solid) lwidth(medium))(line Zero Years, lcolor(black)), xlabel(-12(1)12, nogrid labsize(large)) legend(off) ytitle("{&Delta} P(Notice of Violation)", size(large)) ylabel(-0.02(0.02)0.1,labsize(large)) xtitle("Month", size(large)) graphregion(color(white)) plotregion(color(white)) xsize(8.6)
graph export "$figures/Figure_A8_Panel_A.pdf", replace

restore

//NOE as outcome, complaint air inv as treatment
use "$processed_data/Air_Panel_Investigated_Clean_ever_air_incident.dta", clear
keep if ever_air_incident==1
// Reinitialize 
cap drop b u d se Years Zero
gen Years = _n-13 if _n<=25
gen Zero = 0 if _n <=25
gen b = 0
gen se = 0
gen u = 0 
gen d = 0
//Regressions for post-complaint
foreach h in 0 1 2 3 4 5 6 7 8 9 10 11 12 {
reghdfe p_air_noe_`h'  p_air_complaint_inv, absorb(RN_year t) cluster(RN_id)
replace b = _b[p_air_complaint_inv] if Years == `h'
replace se = _se[p_air_complaint_inv] if Years == `h'
replace u = (_b[p_air_complaint_inv] + 1.96*_se[p_air_complaint_inv]) if Years == `h'
replace d = (_b[p_air_complaint_inv] - 1.96*_se[p_air_complaint_inv]) if Years == `h'
}

//Regressions for pre-complaint
foreach h in 2 3 4 5 6 7 8 9 10 11 12 {
reghdfe p_air_noe_neg`h'  p_air_complaint_inv, absorb(RN_year t) cluster(RN_id)
replace b = _b[p_air_complaint_inv] if Years == -`h'
replace se = _se[p_air_complaint_inv] if Years == -`h'
replace u = (_b[p_air_complaint_inv] + 1.96*_se[p_air_complaint_inv]) if Years == -`h'
replace d = (_b[p_air_complaint_inv] - 1.96*_se[p_air_complaint_inv]) if Years == -`h'
}
preserve
keep if Years != .
keep b u d se Years Zero
export delimited "$point_estimates/Point_Estimates_Figure_A8_Panel_C.csv", replace
graph set window fontface "Times New Roman"
twoway(rarea u d Years, col(navy) fint(inten20) lwidth(0) lpattern(solid))(line b Years, lcolor(navy) lpattern(solid) lwidth(medium))(line Zero Years, lcolor(black)), xlabel(-12(1)12, nogrid labsize(large)) legend(off) ytitle("{&Delta} P(Notice of Enforcement)", size(large)) ylabel(-0.01(0.01)0.02,labsize(large)) xtitle("Month", size(large)) graphregion(color(white)) plotregion(color(white)) xsize(8.6)
graph export "$figures/Figure_A8_Panel_C.pdf", replace

restore

// Import dataset
use "$processed_data/Panel_inv_types_onsite_included_final.dta", clear
keep if ever_complaint==1
cap drop b u d se Years Zero
gen Years = _n-13 if _n<=25
gen Zero = 0 if _n <=25
gen b = 0
gen se = 0
gen u = 0 
gen d = 0

// Regressions for noncomplaint outcomes (0 to 12 months after) for NOV
foreach h in 0 1 2 3 4 5 6 7 8 9 10 11 12 {
reghdfe p_air_nov_`h'  p_air_noncomp_onsite, absorb(RN_year t) cluster(RN_id )
unique t if e(sample)
replace b = _b[p_air_noncomp_onsite] if Years == `h'
replace se = _se[p_air_noncomp_onsite] if Years == `h'
replace u = (_b[p_air_noncomp_onsite] + 1.96*_se[p_air_noncomp_onsite]) if Years == `h'
replace d = (_b[p_air_noncomp_onsite] - 1.96*_se[p_air_noncomp_onsite]) if Years == `h'
}

// Regressions for pre-noncomplaint  (2-12 months before) for NOV
foreach h in 2 3 4 5 6 7 8 9 10 11 12 {
reghdfe p_air_nov_neg`h'  p_air_noncomp_onsite, absorb(RN_year t) cluster(RN_id)
unique t if e(sample)
replace b = _b[p_air_noncomp_onsite] if Years == -`h'
replace se = _se[p_air_noncomp_onsite] if Years == -`h'
replace u = (_b[p_air_noncomp_onsite] + 1.96*_se[p_air_noncomp_onsite]) if Years == -`h'
replace d = (_b[p_air_noncomp_onsite] - 1.96*_se[p_air_noncomp_onsite]) if Years == -`h'
}
keep if Years != .
keep b u d se Years Zero
export delimited "$point_estimates/Point_Estimates_Figure_A8_Panel_B.csv", replace
graph set window fontface "Times New Roman"
twoway(rarea u d Years, col(navy) fint(inten20) lwidth(0) lpattern(solid))(line b Years, lcolor(navy) lpattern(solid) lwidth(medium))(line Zero Years, lcolor(black)), xlabel(-12(1)12, nogrid labsize(large)) ylabel(-0.02(0.02)0.1, labsize(large)) legend(off) ytitle("{&Delta} P(Notice of Violation)", size(large)) xtitle("Month", size(large)) graphregion(color(white)) plotregion(color(white)) xsize(8.6)

//Save figure
graph export "$figures/Figure_A8_Panel_B.pdf", replace


use "$processed_data/Panel_inv_types_onsite_included_final.dta", clear
keep if ever_complaint==1
//Reinitalize
cap drop b u d se Years Zero
gen Years = _n-13 if _n<=25
gen Zero = 0 if _n <=25
gen b = 0
gen se = 0
gen u = 0 
gen d = 0

// Regressions for noncomplaint outcomes for NOE
foreach h in 0 1 2 3 4 5 6 7 8 9 10 11 12 {
reghdfe p_air_noe_`h'  p_air_noncomp_onsite, absorb(RN_year t) cluster(RN_id )
unique t if e(sample)
replace b = _b[p_air_noncomp_onsite] if Years == `h'
replace se = _se[p_air_noncomp_onsite] if Years == `h'
replace u = (_b[p_air_noncomp_onsite] + 1.96*_se[p_air_noncomp_onsite]) if Years == `h'
replace d = (_b[p_air_noncomp_onsite] - 1.96*_se[p_air_noncomp_onsite]) if Years == `h'
}

// Regressions for pre-noncomplaint  (2-12 months before) for NOE
foreach h in 2 3 4 5 6 7 8 9 10 11 12 {
reghdfe p_air_noe_neg`h'  p_air_noncomp_onsite, absorb(RN_year t) cluster(RN_id)
unique t if e(sample)
replace b = _b[p_air_noncomp_onsite] if Years == -`h'
replace se = _se[p_air_noncomp_onsite] if Years == -`h'
replace u = (_b[p_air_noncomp_onsite] + 1.96*_se[p_air_noncomp_onsite]) if Years == -`h'
replace d = (_b[p_air_noncomp_onsite] - 1.96*_se[p_air_noncomp_onsite]) if Years == -`h'
}
keep if Years != .
keep b u d se Years Zero
export delimited "$point_estimates/Point_Estimates_Figure_A8_Panel_D.csv", replace
graph set window fontface "Times New Roman"
twoway(rarea u d Years, col(navy) fint(inten20) lwidth(0) lpattern(solid))(line b Years, lcolor(navy) lpattern(solid) lwidth(medium))(line Zero Years, lcolor(black)), xlabel(-12(1)12, nogrid labsize(large)) ylabel(-0.01(0.01)0.02, labsize(large)) legend(off) ytitle("{&Delta} P(Notice of Enforcement)", size(large)) xtitle("Month", size(large)) graphregion(color(white)) plotregion(color(white)) xsize(8.6)
// Save figure
graph export "$figures/Figure_A8_Panel_D.pdf", replace

